<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <%@include file="/res/include/base-head.jsp"%>
	<%@include file="/res/include/jquery-easyui.jsp"%>
	<%@include file="/res/include/umeditor.jsp"%>
	<script type="text/javascript" src="${pageContext.request.contextPath}/res/js/mall/brand.js"></script>
  </head>
  <body>  
<div class="page_title clearfix">
    <h2>
    <c:if test="${!empty mallBrandVo}">
    	编辑
    </c:if>
    <c:if test="${empty mallBrandVo}">
    	新建
    </c:if>
    </h2>
</div>
<div class="form_item topic_add">
   <form id="data-form">
   	<c:if test="${!empty mallBrandVo}">
    	<input type="hidden" name="id" value="${mallBrandVo.id }"/>
    </c:if>
   	<dl>
        <dt>分类：</dt>
        <dd id="category">
        	<c:if test="${!empty mallBrandVo}">
        		<span id="editCategory">
	        		<c:forEach var="parentCategory" items="${allParentCategory}" varStatus="status">
			    		${parentCategory.name }<c:if test="${!status.last}">-</c:if>
			    	</c:forEach>
        		 （点击可更改）</span>
		    	<select class="hide" id="category_1" onchange="getChildrenCategory(this.value,1)">
					<option value="">--请选择--</option>
		        	<c:forEach var="categoryVo" items="${mallCategoryVoList}">
		        		<option value="${categoryVo.categoryId }">${categoryVo.name }</option>
		        	</c:forEach>
				</select>
		    </c:if>
		    <c:if test="${empty mallBrandVo}">
		    	<select id="category_1" onchange="getChildrenCategory(this.value,1)">
					<option value="">--请选择--</option>
		        	<c:forEach var="categoryVo" items="${mallCategoryVoList}">
		        		<option value="${categoryVo.categoryId }">${categoryVo.name }</option>
		        	</c:forEach>
				</select>
		    </c:if>
        </dd>
    </dl>
    <dl>
        <dt>名称：</dt>
        <dd><input type="text" name="name" value="${mallBrandVo.name }" class="txt grid-25 easyui-validatebox" data-options="required:true" maxlength="32"/></dd>
    </dl>
    <dl>
        <dt>状态：</dt>
        <dd>
        	<select name="status">
				<option value="1"
					<c:if test="${mallBrandVo != null && mallBrandVo.status==1}"> selected="selected"</c:if>
				>有效</option>
				<option value="2"
					<c:if test="${mallBrandVo != null && mallBrandVo.status==2}"> selected="selected"</c:if>
				>无效</option>
			</select>
        </dd>
    </dl>
    
    </form>
    <p class="page_btn" id="showBtn"><input type="button" id="doSave" value="保存" class="btn_blue" onclick="doSave();"/></p>
</div>
<script type="text/javascript">
var qlevel = 1;

$(document).ready(function() {
	<c:if test="${!empty mallBrandVo}">
		$('#editCategory').click(function(){
			$('#editCategory').hide();
			$('#category_1').show();
		});
		//$('#category_1').hide();
	</c:if>
});

function getChildrenCategory(parentId,level) {
	if (parentId != "") {
		for (var i = (level+1); i <= qlevel; i++) {
			$('#category_'+i).remove();
		}
		qlevel = level;
		var data = {parentId:parentId}
		$.post(contextPath+'/mall/category/findChildren',data,function(result) {
			if(result.code==1) {
				if (result.map.childrenList && result.map.childrenList.length > 0) {
					$('#showBtn').hide();
					qlevel = level + 1;
					var childrenList = result.map.childrenList;
					var html = [];
					html.push('<select id="category_'+qlevel+'" onchange="getChildrenCategory(this.value,'+qlevel+')">');
					html.push('<option value="">请选择</option>');
					for (var i = 0; i < childrenList.length; i++) {
						html.push('<option value="'+childrenList[i].categoryId+'">'+childrenList[i].name+'</option>');
					}
					html.push('</select>');
					$('#category').append(html.join(""));
				} else {
					$('#showBtn').show();
				}
			}
		});
	}
}

var isSave = false;
//提交保存
function doSave(){
	var url = contextPath+'/mall/brand/add';
	<c:if test="${!empty mallBrandVo}">
		url = contextPath+'/mall/brand/update';
	</c:if>
	var lastCategory = $('#category').find('select:last');
	if(!$('#data-form').form('validate')) return;
	if (isSave) {
		return;
	}
	isSave = true;
	var data = $('#data-form').form('jsonObject');
	if (lastCategory) {
		data.categoryId = $(lastCategory).val();
	}
	//console.info("------------:"+JSON.stringify(data));
	$.post(url,data,function(result) {
		if(result.code==1) {
			top.showInfo('保存成功');
		} else {
			isSave = false;
			top.showInfo('保存失败:'+result.msg);
		}
	},'json');
	/* $.ajax({    
	    url:url,
	    contentType : 'application/json',
	    data:data,    
	    type:'post',    
	    cache:false,    
	    dataType:'json', 
	    success:function(result) {
	    	if(result.code==1) {
				top.showInfo('保存成功');
			} else {
				isSave = false;
				top.showInfo('保存失败:'+result.msg);
			}
	     },
	     error:function(XMLHttpRequest, textStatus, errorThrown){
	    	 isSave = false;
	    	 top.showInfo(errorThrown);
	     }
	}); */
}
</script>
  </body>
</html>
